<template>
  <div class="m-l-20 m-t-20 w-500">
<!--    <span v-html="$xss(form.content)"></span>-->
    <el-form ref="form" :model="form" :rules="rules" label-width="60px">
      <el-form-item label="标题" prop="title">
        <el-input v-model.trim="form.title" class="h-40 w-200" :maxlength=12></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="category_id">
        <el-select v-model="form.category_id" placeholder="请选择文章分类" class="w-200">
          <el-option v-for="item in categoryOptions" :label="item.title" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="内容" prop="content" style="margin-left: 0px;;">
        <quill-editor-qiniu style="min-width: 720px;margin-left: 0px !important" class="myQuillEditor"
                            @changeContent="getContentFormSon"
                            :detailContent="form.content"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save('form')" :loading="isLoading">提交</el-button>
        <el-button @click="goback()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style>
  .form-checkbox:first-child{
    margin-left: 15px;
  }
</style>
<script>
  import http from '../../../assets/js/http'
  import fomrMixin from '../../../assets/js/form_com'
  import _ from 'lodash'
  import _g from '@/assets/js/global'
  import { Button, Dialog, Form, FormItem, Input, Select, Option, CheckboxGroup, Checkbox } from 'element-ui'
  import quillEditorQiniu from '@/components/Common/quill-editor-qiniu.vue'
  import VueXss from 'vue-xss'

  Vue.use(Button)
  Vue.use(Form)
  Vue.use(FormItem)
  Vue.use(Input)
  Vue.use(Select)
  Vue.use(Option)
  Vue.use(CheckboxGroup)
  Vue.use(Checkbox)
  Vue.use(VueXss)

  export default {
    data() {
      return {
        isLoading: false,
        id: null,
        form: {
          title: '',
          content: '',
          category_id: null,
          remark: '',
          group: []
        },
        password: '',
        categoryOptions: [],
        groupOptions: [],
        selectedGroup: [],
        selectedIds: [],
        rules: {
          title: [
            { required: true, message: '请输入标题' }
          ],
          content: [
            { required: true, message: '请输入内容' }
          ],
          category_id: [
            { required: true, message: '请选择文章分类' }
          ]
        }
      }
    },
    methods: {
      getContentFormSon(data) {
        this.form.content = data
      },
      save(form) {
        if (this.$route.params.id) {
          this.edit(form)
        } else {
          this.add(form)
        }
      },
      selectCheckbox() {
        let temp = false
        _(this.groupOptions).forEach((res) => {
          if (this.selectedGroup.toString().indexOf(res.else) > -1) {
            this.selectedIds.push(res.id)
          }
        })
        if (this.selectedIds.length) {
          this.form.group = _.cloneDeep(this.selectedIds)
          temp = true
        }
        this.selectedIds = []
        return temp
      },
      add(form) {
        this.$refs.form.validate((pass) => {
          if (pass) {
            this.isLoading = true
            this.apiPost('admin/article', this.form).then((res) => {
              this.handelResponse(res, (data) => {
                this.isLoading = false
                _g.toastMsg('success', '添加成功')
                _g.clearVuex('setUser')
                setTimeout(() => {
                  this.goback()
                }, 1500)
              }, () => {
                this.isLoading = false
              })
            })
          }
        })
      },
      edit() {
        this.$refs.form.validate((pass) => {
          if (pass) {
            this.isLoading = true
            if (this.password) {
              this.form.password = this.password
            }
            this.apiPut('admin/article/', this.id, this.form).then((res) => {
              this.handelResponse(res, (data) => {
                this.isLoading = false
                _g.toastMsg('success', '编辑成功')
                _g.clearVuex('setUser')
                setTimeout(() => {
                  this.goback()
                }, 1500)
              }, () => {
                this.isLoading = false
              })
            })
          }
        })
      },
      getAllCategory() {
        this.apiGet('admin/category', { 'personal': 1 }).then((res) => {
          this.handelResponse(res, (data) => {
            this.categoryOptions = data.list
          })
        })
      },
      async getCompleteData() {
        this.apiGet('admin/article/' + this.id).then((res) => {
          this.handelResponse(res, (data) => {
            this.form.title = data.title
            this.form.content = data.content
            this.form.category_id = data.category_id
            this.form.remark = data.remark
            _(data.group).forEach((res1) => {
              _(this.groupOptions).forEach((res2) => {
                if (res1.title == res2.else) {
                  this.selectedGroup.push(res1.title)
                }
              })
            })
          })
        })
      }
    },
    created() {
      this.getAllCategory()
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        // this.$router.push({ path: this.$route.path, query: { id: this.id }})
        this.getCompleteData()
      }
    },
    components: {
      quillEditorQiniu
    },
    mixins: [http, fomrMixin]
  }
</script>
